(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.intindex.listController', listController);
	listController.$inject = [ '$scope', '$q', 'Constants', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService',
			'intindexService' ];
	function listController($scope, $q, Constants, gridService, modalService, layoutConfig, validatorConfig, authService, intindexService) {
		var ctrl = this, log = $scope.loggerFactory.getLogger("intindexList");
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		
		ctrl.init = function() {
			ctrl.gridOptions = gridService.createGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, paginationChanged, rowSelectionChanged, sortChanged);
			ctrl.gridOptions.data = 'vm.searchCriteria.list';
			ctrl.searchCriteria = ctrl.gridOptions.defaultSearchCriteria;
			ctrl.search();
		};
		
		ctrl.create = function() {
			$scope.go('cda.intindex.create');
		};
		
		ctrl.intindexHistory = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.intindexhistory',
				'params':{
					'selectedRows' : ctrl.selectedRows,
					
				},
				'btnCallback' : {
					'close' : function(){
						$uibModalInstance.close();
					}
				}
			});
			
		};

		ctrl.intindexDeactivate = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			var s=ctrl.selectedRows[0].intIndexCode.indexOf("^");
			var id=ctrl.selectedRows[0].intIndexCode.substring(0,s);
			modalService.openConfirmDialogs({
				'messages' : [ 'Are you sure you want to deactivate'+' '+ id+'?' ],
				'btnCallback' : {
					'ok' : function(result) {
						return authService.intindexDeactivate(ctrl.selectedRows[0]).then(saveSuccessful, saveFailed);
					},
					'close' : function(result) {
					}
				}
			});
		};
		
		
		
		ctrl.intindexActivate = function() {
			modalService.openCustomDialogs({
				'dialogsModule' : 'cda.intindexactivate',
				'params':{
					'selectedRows' : ctrl.selectedRows,
					
				},
				'btnCallback' : {
					'ok' : function(){
						ctrl.search();
					}
				}
			});
		};
		
		function saveSuccessful(response) {
			ctrl.search();
		}

		function saveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		
		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(
			function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.gridOptions);
			},angular.noop);
		};

		function paginationChanged(pageNum, pageSize) {
			ctrl.searchCriteria.pageNum = pageNum;
			ctrl.searchCriteria.pageSize = pageSize;
			ctrl.searchCriteria.list = [];
			ctrl.search();
		}

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

		function sortChanged(sortColumns) {
			// now just support sort by one column
			ctrl.searchCriteria.sortDirection = sortColumns[0] ? sortColumns[0].direction : '';
			ctrl.searchCriteria.sortBy = sortColumns[0] ? sortColumns[0].sortBy : '';
			ctrl.search();
		}
	}
})(angular);